package com.abaaba.project2.controller;

import com.abaaba.project2.bean.Brand;
import com.abaaba.project2.bean.Category;
import com.abaaba.project2.bean.bo.BrandCreateBo;
import com.abaaba.project2.bean.bo.CategoryCreateBo;
import com.abaaba.project2.bean.bo.CategoryUpdateBo;
import com.abaaba.project2.bean.data.BaseData;
import com.abaaba.project2.bean.vo.*;
import com.abaaba.project2.service.AdminBrandService;
import com.abaaba.project2.service.AdminCategoryService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Autowired
    AdminCategoryService adminCategoryService;

    @RequestMapping("/l1")
    public BaseRespVo l1() {
        List<CategoryLevelOneVo> categoryLevelOneVos = adminCategoryService.queryCategoryByLevel();
        return BaseRespVo.ok(categoryLevelOneVos);
    }
    @RequestMapping("/list")
    public BaseRespVo list() {
        List<CategoryVo> categoryVos = adminCategoryService.queryCategory();
        return BaseRespVo.ok(categoryVos);
    }


    @RequestMapping("/create")
    public BaseRespVo create(@RequestBody CategoryCreateBo categoryCreateBo) {
        CategoryCreateVo categoryCreateVo = adminCategoryService.insertCategory(categoryCreateBo);
        return BaseRespVo.ok(categoryCreateVo);
    }

    @RequestMapping("/update")
    public BaseRespVo update(@RequestBody CategoryUpdateBo categoryCreateBo) {
        if ("L2".equals(categoryCreateBo.getLevel()) && categoryCreateBo.getPid() == null){
            return BaseRespVo.fail("参数错误");
        }
        adminCategoryService.updateCategory(categoryCreateBo);
        return BaseRespVo.ok();
    }

    @RequestMapping("/delete")
    public BaseRespVo delete(@RequestBody Category category) {
        adminCategoryService.deleteCategory(category);
        return BaseRespVo.ok();
    }

}
